package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Introduce extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;
    private SQLiteDatabase db;
    private String text;
    private int PointImage;
    private boolean IsCollection;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduce);

        Button makePlan = (Button) findViewById(R.id.make_plan);
        makePlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Introduce.this, Plan.class);
                startActivity(intent);
            }
        });


        Intent intent = getIntent();
        String data = intent.getStringExtra("extra_data");


        TextView title = (TextView) findViewById(R.id.introduce_title);
        title.setText(data);



        dbHelper = new MyDatabaseHelper(this, "Records.db", null, 2);
        db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("Record", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));

                if (data.equals(name)) {
                    text = cursor.getString(cursor.getColumnIndex("text"));
                    PointImage = cursor.getInt(cursor.getColumnIndex("image"));
                    break;


                }


            } while (cursor.moveToNext());

        }
        cursor.close();


        TextView textView = (TextView) findViewById(R.id.introduce_text);
        textView.setText(text);
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());

        ImageView imageView = (ImageView) findViewById(R.id.point_imagine);
        imageView.setBackgroundResource(PointImage);


        IsCollection=false;
        Cursor cursor1 = db.query("Collection", null, null, null, null, null, null);
        if (cursor1.moveToFirst())
        {
            do {

                String name = cursor1.getString(cursor.getColumnIndex("name"));

                if (data.equals(name)) {
                    IsCollection=true;
                    break;

                }

            } while (cursor1.moveToNext());

        }
        cursor1.close();



        Button add_collection=(Button)findViewById(R.id.add_collction);
        if (IsCollection)
        {
            add_collection.setText("取消收藏");
        }
        else
        {
            add_collection.setText("添加收藏");
        }

            add_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (add_collection.getText().equals("添加收藏"))
                {
                    db=dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("name", data);
                    db.insert("Collection", null, values);
                    add_collection.setText("取消收藏");
                    Toast.makeText(Introduce.this,"添加成功", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    db=dbHelper.getWritableDatabase();
                    db.delete("Collection","name=?",new String[]{data});
                    add_collection.setText("添加收藏");
                    Toast.makeText(Introduce.this,"取消成功", Toast.LENGTH_SHORT).show();
                }


            }
        });


    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}